

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
html, body, #map-canvas { height: 100%; margin: 0; padding: 0;}
</style>
<script
src="https://maps.googleapis.com/maps/api/js?language=en?key=YOUR_API_KEY"> <!-- Assigned to my Google account -->
<!-- Look closely. Path? -->
</script>
</head>
<body>
<div id="map-canvas"></div>
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: { lat: 50.671441, lng: -120.36273}, // TRU
zoom: 14
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); // Where is google defined?
}
google.maps.event.addDomListener(window, 'load', initialize); // Where is google.maps defined?
</script>
</body>
</html>
| Create/open/new | HTTP POST | Supported in HTML5 | |
| Read/get/retrieve | HTTP GET | Supported in HTML5 | |
| Update/change | HTTP PUT | Not supported in HTML5 | How to send the HTTP PUT data? |
| Delete/close | HTTP DELETE | Not supported in HTML5 | How to send the HTTP DELETE data? |
/tokens/{id}, not /getToken, where {...} just means a parameter or comma separated parameters.../tokens/12, not {12}.
/tokens or /tokens/{id}/value for a singleton resouce/tokens, /tokens/{id}, /tokens/{id}/value/tokens/{id1,id2}/tokens?username=David/tokens?username=David-Williams, not /Tokens?Username=David_Williams/tokens/

| Create/open/new | HTTP POST | Supported in HTML5 | |
| Read/get/retrieve | HTTP GET | Supported in HTML5 | |
| Update/change | HTTP PUT | Not supported in HTML5 | How to send the HTTP PUT data? |
| Delete/close | HTTP DELETE | Not supported in HTML5 | How to send the HTTP DELETE data? |
/tokens/{id}, not /getToken/tokens or /tokens/{id}/value for a singleton resouce/tokens, /tokens/{id}, /tokens/{id}/value/tokens/{id1},{id2}/tokens?username=David/tokens?username=David-Williams, not /Tokens?Username=David_Williams/tokens/POST /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'??? /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'??? /tokens?username=...&password=...'{"tokenid":"...", "explanaton":"..."}', where if tokenid is negative, then error??? /tokens/{tokenid}'{"result":"true"|"false", "explanation":"..."}'??? /collections?tokenid=...&name=...'{"collectionid":"...", "explanaton":"..."}', where if collectionid is negative, then error??? /collections/{collection_id}?tokenid=...&document=..., where the document value should be a JSON string.
'{"result":"true"|"false", "explanation":"..."}'??? /collections/{collection_id}?tokenid=...&query=..., where the query value should be a JSON string for MongoDB query.
'{"result":"true"|"false", "documents":"...", "explanation":"..."}', where the documents value is a JSON string of an array of found documents.??? /???/{???}?tokenid=...&query=...&document=...'{"result":"true"|"false", "explanation":"..."}'??? /???/{???}?tokenid=...&query=...'{"result":"true"|"false", "explanation":"..."}'var db = new TRUMongoDBService(host) - Connect to the server .open().collection().close()db.register(username, password, callback(result)) - Register a new userdb.unsubscribe(username, password, callback(result)) - Delete a userdb.open(username, password, callback(result)) - Sign in; Connectiondb.collection(collection_name, callback(result)) - Select a collectiondb.insert(document, callback(result)),
db.find(query, callback(result)),
db.update(query, document, callback(result))
db.delete(query, callback(result)),
db.close() - Close the connectiontoken_id and collection_id are hidden and not included in the above APIapp.METHOD(PATH, HANDLER); // METHOD: a HTTP request method; PATH: path in URL
const express = require('express'); // "Express" framework
const app = express();
const port = ???; // Use your port number.
// Allow Cross-domain requests, i.e., CORS (Cross-Origin Resource Sharing)
// Why do we need this?
app.all(/.*/, function(req, res, next) { // all(): any HTTP method; /.*/: any routes
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); // Default: only GET and POST
next(); // Express middleware function; To continue to next operations
});
// A route with GET method and '/'
app.get('/', (req, res) => {
res.send('GET: Hello World!') // send(): combination of write() and end()
})
// A route with POST method and '/'
app.post('/', (req, res) => {
res.write('POST: Hello World!')
res.end()
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
| POST, PUT, DELETE | GET |
|---|---|
| express.json(); req.body.queryname | req.query.queryname |
| req.params.id | req.params.id |
const express = require('express')
const app = express()
app.use(express.urlencoded({extended:false}));
app.use(express.json());
const port = ??? // Use your port number.
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
// For CORS
...
// A route with GET method and '/'
app.get('/', (req, res) => {
res.send('GET: Hello World!') // send(): combination of write() and end()
})
// A route with POST method and '/'
app.post('/', (req, res) => {
res.write('POST: Hello World!')
res.end()
})
// A route with GET method and '/users/'
app.get('/users', (req, res) => {
res.send('GET: username=' + req.???.username + "; password=" + ????)
})
// A route with POST method and '/users'
app.post('/users', (req, res) => {
res.send('POST: username=' + req.???.username + "; password=" + ????)
})
req.params.id to get a parameter or parameters.
Here is an example.
const express = require('express')
const app = express()
const port = ??? // Use your port number.
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
...
// GET method with '/tokens/{id}'
app.get('/tokens/:id', (req, res) => { // Be careful with ':'
res.send("GET: /tokens; id=" + req.???.id);
})
// POST method with '/tokens/{id}'
app.post('/tokens/:id', (req, res) => {
res.send("POST: /tokens; id=" + req.???.id);
})
{username:..., password:..., collectionname:...}, ...
{token:...},{tokenid:..., username:...}, ...
ready(callback)
true or false
close()
usernameExists(username, callback)
true or false
validateUsernamePassword(username, password, callback)
true or false
registerUser(username, password, callback)
true or false
deleteUser(username, password)
nothing to return
getNewToken(username, callback) // Create/Open a connection
(true, token id), or
(false, -1) // error case
deleteToken(token_id, callback) // Delete/Close a connection
true or false
collection(token_id, collection_name, callback) // use collection_name for token_id
(true, 1), or
(false, -1) or (false, -2)
insertOne(token_id, doc, callback) // insert one document
true or false
find(token_id, query, callback) // find documents
(true, result) or // result: array of documents
false
updateMany(token_id, query, newdoc, callback) // update documents
true or false
deleteMany(token_id, query, callback) // delete documents
true or false
ready() => registerUser()ready() => deleteUser()ready() => validateUsernamePassword() => getNewToken()ready() => deleteToken() => close()
.../current_working_directory/
- node_modules You may need to install 'mongodb@4.17.2' and 'express'.
- model.js You need to download this file.
- rest_server.js You need to write this program.
const express = require('express');
const model = require('./model.js');
const app = express();
const server = app.listen(???, function () { // User your port number
let host = server.address().address;
let port = server.address().port;
console.log("Listening at http://%s:%s", host, port)
})
// for POST, PUT, and DELETE query
app.use(express.urlencoded({extended:false}));
app.use(express.json());
// To support CORS
app.all(/.*/, function(req, res, next) { // /.*/: any routes
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); // Default: only GET and POST
next(); // Express middleware function; To continue to next operations
});
// Route operations
app.get('/', function (req, res) { // HTTP GET method
res.send("Welcome to TRU MongoDB Web Service!");
})
// SignUp (Create/Register a user)
// Request - POST /users?username=...&password=...
// Response - '{"result":"true"|"false", "explanation":"..."}'
app.post('/users', function (req, res) { // HTTP POST method
var username = req.body.username;
var password = req.body.password;
console.log("POST /users: username = %s, password = %s", username, password);
model.ready(function(result) {
if (result) {
model.registerUser(username, password, function(result) {
if (result)
res.send(JSON.stringify({result:true, explanation:""}));
else
res.send(JSON.stringify({result:false, explanation:"Username exists"}));
});
}
else
res.send(JSON.stringify({result:false, explanation:"Connection error"}));
});
});
...
rest_server.js that supports SignUp and Unsubscribe, and test it.
Here are the related REST API messages that are submitted to the server.
??? /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'??? /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'model.js:
ready(callback)
true or false
registerUser(username, password, callback)
true or false
deleteUser(username, password)
nothing to return
ready() => registerUser()ready() => deleteUser()rest_server.js that supports SignIn and Close, and test it.
Here are the related REST API messages that are submitted to the server.
??? /tokens?username=...&password=...'{"tokenid":"...", "explanaton":"..."}', where if tokenid is negative, then error??? /???/{tokenid}'{"result":"true"|"false", "explanation":"..."}'model.js:
ready(callback)
true or false
close()
validateUsernamePassword(username, password, callback)
true or false
getNewToken(username, callback) // Create/Open a connection
(true, token id), or
(false, -1) // error case
deleteToken(token_id, callback) // Delete/Close a connection
true or false
ready() => validateUsernamePassword() => getNewToken()ready() => deleteToken() => close()rest_server.js with Demo for TRU MongoDB Web Service with your port server number.
rest_server.js to complete TRU MongoDB Web Service.??? /collections?tokenid=...&name=...'{"collectionid":"...", "explanaton":"..."}', where if collectionid is negative, then error??? /collections/{collection_id}?tokenid=...&document=..., where the document value should be a JSON string.
'{"result":"true"|"false", "explanation":"..."}'??? /collections/{collection_id}?tokenid=...&query=..., where the query value should be a JSON string for MongoDB query.
'{"result":"true"|"false", "documents":"...", "explanation":"..."}', where the documents value is a JSON string of an array of found documents.??? /???/{???}?tokenid=...&query=...&document=...'{"result":"true"|"false", "explanation":"..."}'??? /???/{???}?tokenid=...&query=...'{"result":"true"|"false", "explanation":"..."}'var db = new TRUMongoDBWebService(host) - Connect to the server .open().collection().close()db.register(host, username, password, function(result) {...}) - Register a new userdb.unsubscribe(host, username, password, function(result) {...}) - Delete a userdb.open(host, username, password, function(result) {...}) - Sign indb.collection(collection_name, function(result) {...}) - Select a collectiondb.insert(document, function(result) {...}), // document and query are JSON strings of objects
db.find(query, function(result) {...}), // result is an object
db.update(query, document, function(result) {...})
db.delete(query, function(result) {...}),
db.close() - Close the connectiontoken_id and collection_id are not included in the above APITRUMongoDBWebService()
function TRUMongoDBWebService() {
this.host = ""; // with open()
this.connection_token_id = -1;
this.collection_name = "";
this.collection_id = -1;
this.register = function(host, username, password, callback) {
...
}
this.unsubscribe = function(host, username, password, callback) {
...
}
...
}
register()
this.register = function(host, u, p, callback) {
$.ajax({
url: host + "/users", // POST /users?username=...&password=...
method: "post",
data: { username: u, password: p },
success: function(data) {
data = JSON.parse(data);
callback(data);
}
});
}
POST /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'DELETE /users?username=...&password=...'{"result":"true"|"false", "explanation":"..."}'POST /tokens?username=...&password=...'{"tokenid":"...", "explanaton":"..."}', where if tokenid is negative, then errorDELETE /tokens/{tokenid}'{"result":"true"|"false", "explanation":"..."}'